package huizhi.net;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * 客户端
 *
 * @author YuXD
 * @since 2023/12/25
 */
public class SocketClient2 {

    public static void main(String[] args) throws IOException {
        System.out.println("我是SocketClient");

        // 连接本机的8888端口
        Socket client = new Socket(InetAddress.getLocalHost(), 8888);
        // 从套接字获取字节输出流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        bufferedWriter.write("hello, Server！");
        bufferedWriter.newLine();
        bufferedWriter.flush();


        // 获取客户端输入字节流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String respMsg = bufferedReader.readLine();
        System.out.println(respMsg);

        bufferedWriter.close();
        bufferedReader.close();
        client.close();
    }

}
